<?php

namespace app\common\model;

use think\facade\Cache;
use think\Model;

class GiftModel extends Model
{
    protected $connection = 'db_live';

    protected static function init()
    {
        GiftModel::afterInsert(function ($user) {
            Cache::store('redis')->rm('GIFTLIST:');
            Cache::rm('gift_list');
        });
        GiftModel::afterUpdate(function ($user) {
            Cache::store('redis')->rm('GIFTLIST:');
            Cache::rm('gift_list');
        });
        GiftModel::afterDelete(function ($user) {
            Cache::store('redis')->rm('GIFTLIST:');
            Cache::rm('gift_list');
        });
    }

    public static function lists(): array
    {
        return Cache::remember('gift_list', function () {
            return static::select()->toArray();
        });
    }
}